package com.bw.pack

import android.os.Build
import android.os.ext.SdkExtensions
import androidx.annotation.RequiresApi
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.base.constants.RoutePath
import com.bw.base.view.BaseActivity
import com.bw.image.GlideImageLoader
import com.bw.pack.databinding.ActivitySplashBinding

import com.tencent.mmkv.MMKV
import java.util.*

/**
 * 启动页面
 * 业务功能
 * 1.App介绍（轮播图片，动画，视频）
 * 2.接入广告（开屏广告）
 * 3.第一次安装只会展示app功能，二次打开就有广告
 * 4.选择兴趣标签，为后期做内容推荐
 */
@Route(path = RoutePath.PATH_SPLASH)
class SplashActivity : BaseActivity<ActivitySplashBinding>() {
    private lateinit var myTimer: Timer


    private fun gotoMain() {

        ARouter.getInstance().build(RoutePath.PATH_MAIN).navigation()
        finish()
    }

    private var time: Int = 5

    @RequiresApi(Build.VERSION_CODES.M)
    override fun initView() {


        requestPermissions(
            arrayOf(
                android.Manifest.permission.WRITE_APN_SETTINGS,
                android.Manifest.permission.READ_MEDIA_AUDIO
            ),101
        )

        GlideImageLoader.loadResImage(this, com.bw.common.R.drawable.welcome,binding.ivImg)


        val isFirstBoot = MMKV.defaultMMKV().decodeBool("isFirstBoot", true)
        if(isFirstBoot){
            //第一次进入，显示引导页
            MMKV.defaultMMKV().encode("isFirstBoot", false)
        }else{
            //不是第一次进入，显示广告

        }

        myTimer = Timer()
        myTimer.schedule(object : TimerTask() {
            override fun run() {
                runOnUiThread {
                    binding.tvTime.text = time.toString()
                }
                if (time <= 0) {
                    gotoMain()
                }
                time--
            }
        }, 0,1000)

        binding.tvTime.setOnClickListener {
            gotoMain()
        }

        binding.tvSkip.setOnClickListener {
            gotoMain()
        }

    }

    override fun initData() {
    }

    override fun onDestroy() {
        super.onDestroy()
        if(myTimer!=null){
            myTimer.cancel()
        }
    }

    override fun onStart() {
        super.onStart()
        reportFullyDrawn()
    }


}